//
//  IssuesGameAppDelegate.m
//  IssuesGame
//
//  Created by Martin Grider on 10/2/09.
//  Copyright 2009 Regents of the University of Minnesota
//

#import "IssuesGameAppDelegate.h"


@implementation IssuesGameAppDelegate

@synthesize window;
@synthesize navigationController;
@synthesize gameState;
@synthesize gameController;


#pragma mark -
#pragma mark Application lifecycle

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
	// main window
	window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
	
	gameState = [[GameState alloc] initFromSavedGame];

	if ( ! gameState ) {
		//NSLog( @"No saved game state, starting over..." );
		[self reloadGameStateFromXML];
	}

	// initialize our view controllers
	gameController = [[GameViewController alloc] initWithNibName:nil bundle:nil];
	[gameController setupInitialGameState:gameState];

    // Override point for customization after app launch
	navigationController = [[UINavigationController alloc] initWithRootViewController:gameController];
	[navigationController setNavigationBarHidden:YES];

	[window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
}


- (void)reloadGameStateFromXML {
	NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"gamedocs/questions.xml"];
	NSData *data = [[NSData alloc] initWithContentsOfFile: path];
	gameState = [[GameState alloc] initWithData: data];
	[gameState save];
	[data release];
}


- (void)startNewGame {
	[self reloadGameStateFromXML];
	[gameController setupInitialGameState:gameState];
}


#pragma mark -
#pragma mark ApplicationWill...
- (void)applicationWillTerminate:(UIApplication *)application {
	// Save data if appropriate
	[gameState save];
}


- (void)applicationWillResignActive:(UIApplication *)application {
	[gameState save];
}


#pragma mark -
#pragma mark Memory management

- (void)dealloc {
	[navigationController release];
	[gameState release];
	[gameController release];
	[window release];
	[super dealloc];
}


@end

